//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "common\fn_createGroup.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);

private ["_grp","_side","_recycled"];
DEFAULT_PARAM(_side,0,civilian);
_grp = grpNull;
if (IS_GROUP(_side)) then {
	_grp = _side;
} else {
	_recycled = [];
	if !(isNil "SERVER") then {
		_recycled = SERVER getVariable [QGVAR(RECYCLED_GROUPS_) + str(_side),[]];
		_recycled = _recycled - [grpNull];
		SERVER setVariable [QGVAR(RECYCLED_GROUPS_) + str(_side),_recycled,true];
	};
	if (count _recycled > 0) then {
		_grp = _recycled select 0;
		_recycled set [0, grpNull];
		_recycled = _recycled - [grpNull];
		_grp setVariable [QGVAR(RECYCLED),nil,true];
		TRACE_2("Recycled",count _recycled, count(units _grp));
		if (count(units _grp) > 0) then {
			{
				deleteVehicle _x;
			} foreach units _grp;
			deleteGroup _grp;
			_grp = grpNull;
		};
	};
	if (isNull _grp) then {
		_grp = createGroup _side;
		_grp setVariable [QGVAR(CREATED), time + 120];
		_grp setVariable [QGVAR(SMOKEUNITS), []];
	};
};
TRACE_1("End",_grp);
_grp
